<?php

class ContentController extends Controller
{
    public function actionMore()
    {
        $offset = Yii::app()->request->getParam('offset', microtime(true));
        $extraCriteria = Yii::app()->request->getParam('extraCriteria', '');
        $extraParams = Yii::app()->request->getParam('extraParams', '');
		
        $contents = CommonHelper::getModelsByUrlParams($offset, $extraCriteria, $extraParams, Content::model());
        if( ! empty($contents))
        {
            echo(json_encode(array(
                'result'=>true,
                'html'=>trim($this->renderPartial('//common/_content_list_item', array('contents'=>$contents), true)),
                'moreUrl'=>Content::getMoreUrl($contents, $extraCriteria, $extraParams),
            )));
        }
        else
        {
            echo(json_encode(array(
                'result'=>false,
            )));
        }
    }
    
    public function actionSubmitQuestion()
    {
        $params = Yii::app()->request->getParam('Content');
        if( ! isset($params['id']))
        {
            echo(json_encode(array(
                'result'=>false,
                'errors'=>array('id is missing'),
            )));
        }
        
        if(empty($params['choiceIds']))
        {
            echo(json_encode(array(
                'result'=>false,
                'errors'=>array('choiceIds is mmissing'),
            )));
        }
        $oneChoice = OneChoice::model()->getById($params['id']);
        if($oneChoice instanceof OneChoice)
        {
            $choiceId = array_pop($params['choiceIds']);
            echo(json_encode(array(
                'result'=>$oneChoice->check($choiceId),
                'html'=> $oneChoice->getExplain(),
            )));
        }
    }
}

